﻿using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace 姓名抽奖工具
{
    public partial class Form1 : Form
    {
        private Point mousePoint = new Point();
        private List<string> nameList;
        private bool flag;
        private Random rd;

        public Form1()
        {
            InitializeComponent();
            flag = false;
            rd = new Random();
            this.label4.ForeColor = this.label1.ForeColor = Color.FromArgb(0x55, 0x3a, 0x2f);
            this.label2.ForeColor = Color.FromArgb(0xe8, 0xa2, 0x85);
        }

        /// <summary>
        /// 画面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            //获取当前应用程序所在目录
            string path = System.Windows.Forms.Application.StartupPath.Replace("\\", "/");
            if (!path.EndsWith("/"))
            {
                path += "/";
            }
            path += "name.xlsx";
            if (!File.Exists(path))
            {
                MessageBox.Show("姓名文件不存在,请联系开发人员");
                Environment.Exit(0);
            }
            else
            {
                //读取文件
                nameList = excel2List(path);
            }
        }

        /// <summary>
        /// 退出按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel1_Click(object sender, EventArgs e)
        {
            flag = false;
            Thread.Sleep(50);
            Environment.Exit(0);
        }


        /// <summary>
        /// 鼠标按下去 记录位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            this.mousePoint.X = e.X;
            this.mousePoint.Y = e.Y;
        }


        /// <summary>
        /// 鼠标拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Top = Control.MousePosition.Y - mousePoint.Y;
                this.Left = Control.MousePosition.X - mousePoint.X;
            }
        }

        /// <summary>
        /// 读取Excel
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private List<string> excel2List(string filePath)
        {
            List<string> list = new List<string>();
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
            using (ExcelPackage pck = new ExcelPackage(fs))
            {
                ExcelWorksheet ws = pck.Workbook.Worksheets[0];
                int minRowNum = ws.Dimension.Start.Row; //工作区开始行号
                int maxRowNum = ws.Dimension.End.Row; //工作区结束行号
                for (int i = minRowNum; i <= maxRowNum; i++)
                {
                    object obj = ws.Cells[i, 1].Value;
                    if (obj != null)
                    {
                        string name = obj.ToString();
                        if (!list.Contains(name))
                        {
                            list.Add(name);
                        }
                    }

                }
            }
            return list;
        }

        private void panel3_Click(object sender, EventArgs e)
        {
            if (!flag)
            {
                flag = !flag;
                panel3.BackgroundImage = Properties.Resources.stop;
                new Thread(StartRun).Start();
            }
            else
            {

                flag = !flag;
                panel3.BackgroundImage = Properties.Resources.start;
                int length = nameList.Count;
                label1.InvokeText(nameList[rd.Next(0, length)]);
            }
        }

        private void StartRun()
        {
            int length = nameList.Count;
            while (flag)
            {
                label1.InvokeText(nameList[rd.Next(0, length)]);
                Thread.Sleep(10);
            }
        }
    }
}
